package view;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import controller.Controller;

public class MainWindow
{
	private Display display;
	private Shell shell;
	private Font font;

	public static void main(String[] args)
	{
		MainWindow mainWindow = new MainWindow();
		new Controller(mainWindow);
		mainWindow.run();
	}

	private void run()
	{
		display = new Display();
		font = new Font(display, "Monotype Corsiva", 14, SWT.BOLD);
		shell = new Shell(display);
		shell.setData("font", font);
		shell.setText("3ew Compression");
		Rectangle displayBouns = display.getBounds();
		int width = displayBouns.width / 2;
		int height = displayBouns.height / 2;
		shell.setBounds(displayBouns.x + width/2, displayBouns.y + height/2, width, height);

		createContents();

		shell.open();
		while (! shell.isDisposed())
			if (! display.readAndDispatch())
				display.sleep();
		display.dispose();
	}

	private void createContents()
	{
		shell.setLayout(new FormLayout());
		FormData formData;
		
		formData = new FormData();
		formData.left = new FormAttachment(0, 5);
		formData.top = new FormAttachment(0, 5);
		formData.bottom = new FormAttachment(100, -5);
		formData.right = new FormAttachment(100, -5);

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.heightHint = 30;

		SashForm sashForm = new SashForm(shell, SWT.BORDER);
		sashForm.setLayoutData(formData);

		Composite left = new Composite(sashForm, SWT.NONE);
		left.setLayout(new GridLayout(1, false));


		@SuppressWarnings("unused")
		Composite all = new Composite(sashForm, SWT.NONE);

		sashForm.setWeights(new int[]{1, 1});

		final Text filePath = new Text(left, SWT.READ_ONLY);
		filePath.setLayoutData(gridData);
		filePath.setFont(font);

		Button loadFile = new Button(left, SWT.PUSH);
		loadFile.setLayoutData(gridData);
		loadFile.setText("Load File");
		loadFile.setFont(font);

		Button compress = new Button(left, SWT.PUSH);
		compress.setLayoutData(gridData);
		compress.setText("Compress");
		compress.setFont(font);

		Button decompress = new Button(left, SWT.PUSH);
		decompress.setLayoutData(gridData);
		decompress.setText("De-Compress");
		decompress.setFont(font);

		CLabel compressionRatioLabel = new CLabel(left, SWT.NONE);
		compressionRatioLabel.setFont(font);
		compressionRatioLabel.setText("Compression Ratio: ");

		final CLabel compressionRatio = new CLabel(left, SWT.NONE);
		compressionRatio.setFont(font);
		compressionRatio.setText("                 ");


		loadFile.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				filePath.setText("");
				compressionRatio.setText("");
				FileDialog dialog = new FileDialog(shell);
				dialog.setText("Enter file path");
				String path = dialog.open();
				if (path != null)
				{
					filePath.setText(path);
				}
			}
		});

		compress.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				final String src = filePath.getText();
				if (src.length() != 0 && new File(src).exists())
				{
					ProgressDialog myProgress = new ProgressDialog(shell,
							"Wait pleas; compression is being done");
					shell.setData("progress", myProgress);
					myProgress.open();
						Thread thread = new Thread(new Runnable()
						{
							@Override
							public void run()
							{
								Controller.getInstance().analyse(src);
								String compressed = src.substring(0, src.lastIndexOf('.'));
								Controller.getInstance().compress(src, compressed+Constants.EXTENSION);
							}
						});
						thread.setDaemon(true);
						thread.start();
					Shell dial = (Shell) myProgress.getData("dialog");
					dial.addDisposeListener(new DisposeListener()
					{
						@Override
						public void widgetDisposed(DisposeEvent e)
						{
							double ratio = Controller.getInstance().getCompressionRatio();
							int ratio3Dig = (int) (ratio * 1000);
							ratio = ratio3Dig;
							ratio /= 1000;
							compressionRatio.setText(ratio + "");
						}});
				}
			}
		});

		decompress.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent e)
			{
				final String compressed = filePath.getText();
				if (compressed.length() != 0 && new File(compressed).exists())
				{
					ProgressDialog myProgress = new ProgressDialog(shell,
							"Wait pleas; De-Compression is being done");
					shell.setData("progress", myProgress);
					myProgress.open();
						Thread thread = new Thread(new Runnable() {
							@Override
							public void run() {
	//							String decompressed = compressed.substring(0, compressed.lastIndexOf('.'));
	//							System.out.println(decompressed);
								String compressedPath = compressed.substring(0, compressed.lastIndexOf(File.separatorChar));
								Controller.getInstance().decompress(compressed, compressedPath);
							}
						});
						thread.setDaemon(true);
						thread.start();
				}
		}});
	}

	public void updateProgressBar(final long reached, final long length)
	{
		display.asyncExec(new Runnable()
		{
			@Override
			public void run() {
				ProgressDialog dialog = (ProgressDialog) shell.getData("progress");
				if (dialog.isDisposed())
					return;
				ProgressBar bar = (ProgressBar) dialog.getData("progress");
				if (reached == length)
				{
					bar.getParent().dispose();
				}
				else
					bar.setSelection((int) (((double) reached / length) * bar.getMaximum()));
			}});
	}
}
